package com.space.managegoods.service;


import cn.hutool.core.util.IdUtil;
import com.space.managegoods.mapper.GoodsMapper;
import com.space.managegoods.po.GoodsInfoPO;
import com.space.managegoods.query.GoodsListQuery;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 *
 * @Author yangyf
 * @Date 2023/9/12 0006 16:34
 * @Version 1.0
 */
@Service
public class GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    private static Logger logger = LoggerFactory.getLogger(GoodsService.class);

    public List<GoodsInfoPO> queryList(GoodsListQuery goodsListQuery){
        return goodsMapper.queryGoodsInfo(goodsListQuery);
    }

    @Transactional
    public void initData(List<Map<String,Object>> dataList){
        goodsMapper.cleanData();
        for (int i = 0; i < dataList.size(); i++) {
            Map<String,Object> tmp = dataList.get(i);
            GoodsInfoPO goodsInfoPO = new GoodsInfoPO();
            String goods_type = "";
            String goods_brand = "";
            String goods_model = "";
            float official_price = 0;
            float guide_price = 0;
            for(String key:tmp.keySet()){

                if("品类".equals(key.trim()) ){
                    Object val = tmp.get(key);
                    if(val instanceof Long){
                        goods_type = (long)val + "";
                    }else if(val instanceof String){
                        goods_type = (String) val;
                    }else{
                        logger.error("Excel中第 " +  (i + 1) + "行，列："+ key + "非数字类型");
                    }
                }
                if("品牌".equals(key.trim()) ){
                    Object val = tmp.get(key);
                    if(val instanceof Long){
                        goods_brand = (long)val + "";
                    }else if(val instanceof String){
                        goods_brand = (String) val;
                    }else{
                        logger.error("Excel中第 " +  (i + 1) + "行，列："+ key + "非数字类型");
                    }
                }
                if("型号".equals(key.trim()) ){
                    Object val = tmp.get(key);
                    if(val instanceof Long){
                        goods_model = (long)val + "";
                    }else if(val instanceof String){
                        goods_model = (String) val;
                    }else{
                        logger.error("Excel中第 " +  (i + 1) + "行，列："+ key + "非数字类型");
                    }
                }

                if("官旗价".equals(key.trim()) ){
                    Object val = tmp.get(key);
                    if(val instanceof Long){
                        official_price = (float)(long)val;
                    }else if(val instanceof Double){
                        official_price = (float)(double)val;
                    }else{
                        logger.error("Excel中第 " +  (i + 1) + "行，列："+ key + "非数字类型");
                    }
                }
                if("指导价".equals(key.trim())){
                    Object val = tmp.get(key);
                    if(val instanceof Long){
                        guide_price = (float)(long)val;
                    }else if(val instanceof Double){
                        guide_price = (float)(double)val;
                    }else{
                        logger.error("Excel中第 " +  (i + 1) + "行，列："+ key + "非数字类型");
                    }
                }
            }
            goodsInfoPO.setGoods_type(goods_type);
            goodsInfoPO.setGoods_brand(goods_brand);
            goodsInfoPO.setGoods_model(goods_model);
            goodsInfoPO.setOfficial_price(official_price);
            goodsInfoPO.setGuide_price(guide_price);
            goodsInfoPO.setId(IdUtil.randomUUID());
            goodsInfoPO.setCreate_time(new Date());
            goodsMapper.saveGoodsInfo(goodsInfoPO);
        }
    }
}
